package com.common.widght;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.lsz.deport.R;
import com.util_code.utils.DisplayUtils;

public class IncludeTitleLayout extends Toolbar {

    private ImageView ivLeftImg;
    private TextView tvLeftText;
    private TextView tvToolbarTitle;
    private ImageView ivRightImg;
    private TextView tvRightText;
    private Toolbar titleBar;

    private ToolbarClickListener toolbarClickListener;
    private TextView tvTopLayout;

    public IncludeTitleLayout(Context context) {
        super(context);
        initView(context);
    }

    public IncludeTitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public IncludeTitleLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        View layout = LayoutInflater.from(context).inflate(R.layout.include_title, this, true);
        tvTopLayout = layout.findViewById(R.id.tv_top_layout);
        RelativeLayout rlToolbarLedt = layout.findViewById(R.id.rl_toolbar_left);
        titleBar = layout.findViewById(R.id.title_bar);
        ivLeftImg = layout.findViewById(R.id.iv_left_img);
        tvLeftText = layout.findViewById(R.id.tv_left_text);

        tvToolbarTitle = layout.findViewById(R.id.tv_toolbar_title);

        RelativeLayout rlToobarRight = layout.findViewById(R.id.rl_toobar_right);
        ivRightImg = layout.findViewById(R.id.iv_right_img);
        tvRightText = layout.findViewById(R.id.tv_right_text);

        rlToolbarLedt.setOnClickListener(view -> {
            if (null != toolbarClickListener) {
                toolbarClickListener.onLeftClick();
            }
        });
        rlToobarRight.setOnClickListener(view -> {
            if (null != toolbarClickListener) {
                toolbarClickListener.onRightClick();
            }
        });
    }

    public void setTitleBarHeight(int visibility) {
        tvTopLayout.setVisibility(visibility);
//        LayoutParams params;
//        if (visibility == View.VISIBLE) {
//            rlTopLayout.setVisibility(visibility);
//            params = new LayoutParams(LayoutParams.MATCH_PARENT, DisplayUtils.dp2px(70));
//            titleBar.setPadding(0, DisplayUtils.dp2px(20), 0, 0);
//        } else {
//            rlTopLayout.setVisibility(visibility);
//            params = new LayoutParams(LayoutParams.MATCH_PARENT, DisplayUtils.dp2px(50));
//        }
//        titleBar.setLayoutParams(params);
    }

    public void setTitleBarHeight(int visibility, int paddingTop) {
        LayoutParams params;
        if (visibility == View.VISIBLE) {
            params = new LayoutParams(LayoutParams.MATCH_PARENT, DisplayUtils.dp2px(70));
            titleBar.setPadding(0, DisplayUtils.dp2px(paddingTop), 0, 0);
        } else {
            params = new LayoutParams(LayoutParams.MATCH_PARENT, DisplayUtils.dp2px(50));
        }
        titleBar.setLayoutParams(params);
    }

    public void setToolbarImage(int drawableInt) {
        titleBar.setBackgroundResource(drawableInt);
    }

    public void setLeftImage(int drawableInt) {
        ivLeftImg.setVisibility(View.VISIBLE);
        tvLeftText.setVisibility(View.GONE);
        ivLeftImg.setImageResource(drawableInt);
    }

    public void setLeftText(String leftText) {
        ivLeftImg.setVisibility(View.GONE);
        tvLeftText.setVisibility(View.VISIBLE);
        tvLeftText.setText(leftText);
    }

    public void setTitleText(String toolbarText) {
        tvToolbarTitle.setText(toolbarText);
    }

    public void setTitleText(String toolbarText, int textColor) {
        tvToolbarTitle.setText(toolbarText);
        tvToolbarTitle.setTextColor(getResources().getColor(textColor));
    }

    public void setRightImage(int drawableInt) {
        ivRightImg.setVisibility(View.VISIBLE);
        tvRightText.setVisibility(View.GONE);
        ivRightImg.setImageResource(drawableInt);
    }

    public void setRightText(String rightText) {
        ivRightImg.setVisibility(View.GONE);
        tvRightText.setVisibility(View.VISIBLE);
        tvRightText.setText(rightText);
    }

    public void setToolbarClickListener(ToolbarClickListener toolbarClickListener) {
        this.toolbarClickListener = toolbarClickListener;
    }

    public interface ToolbarClickListener {

        void onLeftClick();

        void onRightClick();
    }

}
